﻿// d.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

//  左值 右值 的使用

#include <iostream>
#include <vector>
using namespace std;

class B
{
public:
	int token;
	B() {
		token = 2;
	}
	B(const B&) {
		token = 100;
		std::cout << "B Constructor" << std::endl; 
	}
};

class A
{
public:
	A() : m_b(new B()) {
		std::cout << "A Constructor" << std::endl; 
	}
	A(const A& src) : m_b(new B(*(src.m_b)))
	{
		std::cout << "A Copy Constructor" << this->m_b->token << std::endl;
	}
	A(A&& src) noexcept : m_b(src.m_b)
	{
		src.m_b = nullptr;
		std::cout << "A Move Constructor" << this->m_b->token << std::endl;
	}
	~A() { delete m_b; }

//private:
	B* m_b;
};

static A getA()
{
	A a;
	std::cout << "========================A========================" << std::endl;
	return a;
}

int main()
{
	A a = getA();
	std::cout << "========================main1========================" << std::endl;
	A a1(a);
	a1.m_b->token++;
	a1.m_b->token++;
	cout << a.m_b->token << "-" << a1.m_b->token << endl;
	std::cout << "=========================main2=======================" << std::endl;

	cout << "addr: " << &(a1.m_b->token) << endl;

	A a2(std::move(a1));
	a2.m_b->token++;
	cout << a.m_b->token  << "-" << a2.m_b->token << endl;
	cout << "addr: " << &(a2.m_b->token) << endl;

	// a1 a2 指向相同的地址空间;
	return 0;
}